package designPatterns.behaviorMode.state;

/**
 * Created by thinkpad on 2015/8/15.
 * People can live with different financial status. They can be rich or they can be poor.
 * The two states - rich and poor - can be converted to each other from time to time.
 * The idea behind the example: people normally work harder when they are poor and play more when they are rich.
 * What they do depends on the state in which they are living. The state can be changed based on their actions,
 * otherwise, the society is not fair.
 */
interface State {
    public void saySomething(StateContext sc);
}

class Rich implements State{
    @Override
    public void saySomething(StateContext sc) {
        System.out.println("I'm rick currently, and play a lot.");
        sc.changeState(new Poor());
    }
}

class Poor implements State{
    @Override
    public void saySomething(StateContext sc) {
        System.out.println("I'm poor currently, and spend much time working.");
        sc.changeState(new Rich());
    }
}

class StateContext {
    private State currentState;

    public StateContext(){
        currentState = new Poor();
    }

    public void changeState(State newState){
        this.currentState = newState;
    }

    public void saySomething(){
        this.currentState.saySomething(this);
    }
}

public class testState {
    public static void main(String args[]){
        StateContext sc = new StateContext();
        sc.saySomething();
        sc.saySomething();
        sc.saySomething();
        sc.saySomething();
    }
}
